package seismosurfer.gui.panel;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import seismosurfer.data.constants.ParameterNames;
import seismosurfer.data.constants.QueryNames;
import seismosurfer.gui.GUIConstants;
import seismosurfer.gui.menu.QueryMenu;
import seismosurfer.layer.QuakeLayer;

import com.bbn.openmap.MapBean;
import com.bbn.openmap.gui.OMComponentPanel;

/**
 * The "Closest-Pairs Query" panel.
 *
 */
public class CPQueryPanel extends OMComponentPanel implements
        PropertyChangeListener, ParameterNames, QueryNames, Serializable,
        GUIConstants {
    
    private static final long serialVersionUID = 7247589130855717402L;

    private MapBean map = null;

    private QuakeLayer ql = null;
    

    protected JSpinner magSpin, pairNumSpin;

    protected SpinnerNumberModel magSpinModel, pairNumSpinModel;

    public CPQueryPanel(ActionListener buttonsListener) {
        super();
        createPanel(buttonsListener);
    }

    protected void createPanel(ActionListener buttonsListener) {

        try {

            // A Panel to hold the controls
            JPanel CPQPane = new JPanel();
            GridBagLayout CPQPaneLayout = new GridBagLayout();
            CPQPane.setLayout(CPQPaneLayout);
            GridBagConstraints c = new GridBagConstraints();

            JLabel Label1 = new JLabel("Number of pairs:");
            c.gridx = 0;
            c.gridy = 0;
            c.gridwidth = GridBagConstraints.REMAINDER;
            CPQPaneLayout.setConstraints(Label1, c);
            CPQPane.add(Label1);

            pairNumSpinModel = new SpinnerNumberModel(2, 0, 10, 1);
            pairNumSpin = new JSpinner(pairNumSpinModel);
            c.gridx = 0;
            c.gridy = 1;
            CPQPaneLayout.setConstraints(pairNumSpin, c);
            CPQPane.add(pairNumSpin);

            JLabel sep1 = new JLabel("  ");
            c.gridx = 0;
            c.gridy = 2;
            CPQPaneLayout.setConstraints(sep1, c);
            CPQPane.add(sep1);

            JLabel Label2 = new JLabel("Set 1 - Epicenters of");
            c.gridx = 0;
            c.gridy = 3;
            CPQPaneLayout.setConstraints(Label2, c);
            CPQPane.add(Label2);

            JLabel Label3 = new JLabel("magnitude greater than");
            c.gridx = 0;
            c.gridy++;
            CPQPaneLayout.setConstraints(Label3, c);
            CPQPane.add(Label3);

            magSpinModel = new SpinnerNumberModel(4.0, 4.0, 8.0, 0.5);
            magSpin = new JSpinner(magSpinModel);
            c.gridx = 0;
            c.gridy++;
            magSpin.setPreferredSize(pairNumSpin.getPreferredSize());
            CPQPaneLayout.setConstraints(magSpin, c);
            CPQPane.add(magSpin);

            JSeparator sep2 = new JSeparator();
            c.gridx = 0;
            c.gridy++;
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(10, 0, 10, 0);
            CPQPaneLayout.setConstraints(sep2, c);
            CPQPane.add(sep2);

            JLabel Label4 = new JLabel("Set 2 - Cities of");
            c.gridx = 0;
            c.gridy++;
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(0, 0, 0, 0);
            CPQPaneLayout.setConstraints(Label4, c);
            CPQPane.add(Label4);

            String[] citiesStrs = { "Greece", "Mediteranean", "World" };
            JComboBox citiesCB = new JComboBox(citiesStrs);
            c.gridx = 0;
            c.gridy++;
            c.gridwidth = 1;
            CPQPaneLayout.setConstraints(citiesCB, c);
            CPQPane.add(citiesCB);

            JSeparator sep3 = new JSeparator();
            c.gridx = 0;
            c.gridy++;
            c.gridwidth = GridBagConstraints.REMAINDER;
            c.insets = new Insets(10, 0, 10, 0);
            CPQPaneLayout.setConstraints(sep3, c);
            CPQPane.add(sep3);

            // bottomPanel consists of 2 buttons: OK and Cancel.
            JPanel bottomPanel = new JPanel();
            bottomPanel.setLayout(new BoxLayout(bottomPanel, BoxLayout.X_AXIS));

            JButton b1 = new JButton("OK");
            b1.addActionListener(buttonsListener);
            b1.setActionCommand(OK);
            bottomPanel.add(b1);

            JButton b2 = new JButton("Cancel");
            b2.addActionListener(buttonsListener);
            b2.setActionCommand(CANCEL);
            bottomPanel.add(b2);

            GridBagLayout contentPaneLayout = new GridBagLayout();
            GridBagConstraints ccP = new GridBagConstraints();
            setLayout(contentPaneLayout);
            ccP.gridx = 0;
            ccP.gridy = 0;
            contentPaneLayout.setConstraints(CPQPane, ccP);
            add(CPQPane);
            ccP.gridx = 0;
            ccP.gridy = 1;
            contentPaneLayout.setConstraints(bottomPanel, ccP);
            add(bottomPanel);
            setLayout(contentPaneLayout);

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println(e.getMessage());
        }

    }

    public void sendQueryParameters() {

        Map parameters = new HashMap();

        // parameters.put(MAG_MIN, (Double) MagSpinModel.getNumber());
        //
        // parameters.put(LONG, (Double) LongSpinModel.getNumber());
        // parameters.put(LAT, (Double) LatSpinModel.getNumber());
        //
        // parameters.put(ROWNUM, (Integer) NofNNSpinModel.getNumber());

        parameters.put(SOURCE, getSource());

        if (ql != null) {
            ql.loadQuakeClientData(QueryNames.NN, parameters);
        } else {
            System.out.println("NULL Layer!!!");
        }

    }   

    public void findAndInit(Object someObj) {

        if (someObj instanceof QuakeLayer) {
            ql = (QuakeLayer) someObj;
        }

        if (someObj instanceof MapBean) {
            map = (MapBean) someObj;
            // setCoordinates();
        }
    }

    public void findAndUndo(Object someObj) {

        if (someObj instanceof MapBean && map == someObj) {
            map = null;
        }
    }

    public void propertyChange(PropertyChangeEvent e) {
        if (e.getPropertyName().equalsIgnoreCase(QueryMenu.SourceProperty)) {
            setSource((String) e.getNewValue());
        }
    }

    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    private String source;

}
